package com.hp.gxw.api;

import com.hp.gxw.constant.R;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.dto.VendorParameter;
import com.hp.gxw.u8.xml.VendorXml;
import com.hp.gxw.utils.DateUtil;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

@Component
@RequestMapping("/api/vendor")
public class VendorApi {

    @Autowired
    private U8ApiUtils u8ApiUtils;

	@Autowired
	private EFWebService eFWebServiceLocator;

    @Value("${u8client.serviceId}")
    private String serviceId;

    @Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R vendor_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        VendorParameter vendorParameter = new VendorParameter();
        if(map.get("code") == null){
            return R.error("code为必填项");
        }
        vendorParameter.setCode(map.get("code").toString());
        if(map.get("name") == null){
            return R.error("name为必填项");
        }
        vendorParameter.setName(map.get("name").toString());
        if(map.get("abbrname") == null){
            return R.error("abbrname为必填项");
        }
        vendorParameter.setAbbrname(map.get("abbrname").toString());
        if(map.get("sort_code") == null){
            return R.error("sort_code为必填项");
        }
        vendorParameter.setSort_code(map.get("sort_code").toString());
        if(map.get("domain_code") != null){
            vendorParameter.setDomain_code(map.get("domain_code").toString());
        }
        if(map.get("tax_reg_code") != null){
            vendorParameter.setTax_reg_code(map.get("tax_reg_code").toString());
        }
        if(map.get("cvenexch_name") != null){
            vendorParameter.setCvenexch_name(map.get("cvenexch_name").toString());
        }else{
            vendorParameter.setCvenexch_name("人民币");
        }
        if(map.get("bank_open") != null){
            vendorParameter.setBank_open(map.get("bank_open").toString());
        }
        if(map.get("legal_man") != null){
            vendorParameter.setLegal_man(map.get("legal_man").toString());
        }
        if(map.get("RegistFund") != null){
            vendorParameter.setRegistFund(map.get("RegistFund").toString());
        }
        if(map.get("bank_acc_number") != null){
            vendorParameter.setBank_acc_number(map.get("bank_acc_number").toString());
        }
        if(map.get("address") != null){
            vendorParameter.setAddress(map.get("address").toString());
        }
        if(map.get("contact") != null){
            vendorParameter.setContact(map.get("contact").toString());
        }
        if(map.get("email") != null){
            vendorParameter.setEmail(map.get("email").toString());
        }
        if(map.get("phone") != null){
            vendorParameter.setPhone(map.get("phone").toString());
        }
        if(map.get("mobile") != null){
            vendorParameter.setMobile(map.get("mobile").toString());
        }
        if(map.get("bvencargo") != null){
            vendorParameter.setBvencargo(map.get("bvencargo").toString());
        }
        if(map.get("bproxyforeign") != null){
            vendorParameter.setBproxyforeign(map.get("bproxyforeign").toString());
        }
        if(map.get("bvenservice") != null){
            vendorParameter.setBvenservice(map.get("bvenservice").toString());
        }
        if(map.get("bvencargo") == null && map.get("bproxyforeign") == null && map.get("bvenservice") == null){
            vendorParameter.setBvencargo("1");
        }
        if(map.get("seed_date") != null){
            vendorParameter.setSeed_date(DateUtil.getDateTimeFormat(map.get("seed_date").toString()));
        }else{
            vendorParameter.setSeed_date(new Date());
        }
        if(map.get("dvencreatedatetime") != null){
            vendorParameter.setDvencreatedatetime(DateUtil.getDateTimeFormat(map.get("dvencreatedatetime").toString()));
        }else{
            vendorParameter.setDvencreatedatetime(new Date());
        }
        if(map.get("CreatePerson") != null){
            vendorParameter.setCreatePerson(map.get("CreatePerson").toString());
        }else{
            vendorParameter.setCreatePerson("api");
        }
        vendorParameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(VendorXml.createXML(vendorParameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(VendorXml.createXML(vendorParameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
